# coding: utf-8

import os
import shutil

'''
    针对php的函数名称和使用方法,采用python封装一次.方便phper直接使用python编程
    感觉像是在使用php一样
'''


def file_exists(path):
    return os.path.exists(path)


def copy(source_file, target_file):
    return shutil.copy(source_file, target_file)


def unlink(path):
    return os.remove(path)


def file_get_contents(path, coding='UTF-8'):
    contents = ''
    with open(path,'r',encoding=coding) as fp:
        contents = fp.read()
    return contents


def file_put_contents(path, content, coding='UTF-8'):
    with open(path,"w",encoding=coding) as fp:
        fp.write(content)
    return content


def is_dir(path):
    return os.path.isdir(path)


def is_file(path):
    return os.path.isfile(path)


def is_link(path):
    return os.path.islink(path)


def is_readable(path):
    return os.access(path,os.R_OK)


def is_writeable(path):
    return os.access(path,os.W_OK)


def rename(source_file, target_file):
    return os.rename(source_file,target_file)


def rmdir(dir_path):
    return os.removedirs(dir_path)


def python_version():
    import platform
    return platform.python_version()


def json_encode(data):
    import json
    return json.dumps(data)


def json_decode(json_str):
    import json
    return json.loads(json_str)


def pi():
    import math
    return math.pi


def trim(string):
    return string.strip()


def ltrim(string):
    return string.lstrip()


def rtrim(string):
    return string.rstrip()


def str_repeat(string, times):
    if type(times) != int or type(string) != str:
        raise Exception('params string is str and times is int')
    return string*times


def strrev(string):
    return string[::-1]


def explode(flag, string):
    return string.split(flag)


def implode(flag, array):
    str_arr = list(map(lambda x:str(x),array))
    return flag.join(str_arr)


def md5(string):
    import hashlib
    return hashlib.md5(string.encode('UTF-8')).hexdigest()


def shell_exec(shell_code):
    return os.system(shell_code)


def time(type=''):
    import time
    get_time = time.time()
    if type == 's':
        return int(get_time)
    elif type == 'ms':
        return int(get_time*1000)
    elif type == 'us':
        return int(get_time*1000*1000)
    else:
        return get_time


def date(timestamp=''):
    from datetime import datetime
    if timestamp == '':
        timestamp = datetime.now().timestamp()
    date_time = datetime.fromtimestamp(timestamp)
    return date_time.strftime('%Y-%m-%d %H:%M:%S')


